package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.ReportItemDto;
import com.csun.cmny.provider.model.domain.OmcReportItem;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcReportItemService;
import com.csun.cmny.provider.validator.OmcReportItemAddValidator;
import com.csun.cmny.provider.validator.OmcReportItemEditValidator;
import com.csun.cmny.provider.vo.ReportItemVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: xcr
 * @Date: 2019-05-25
 * @Description: the class omc ReportItem controller
 */
@RestController
@RequestMapping(value = "/reportItem")
public class OmcReportItemController extends BaseController {

    @Resource
    CmnyMessageSource cmnyMessageSource;

    @Resource
    OmcReportItemService omcReportItemService;

    /*
    * 添加异常报告项
    * */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addService(@Validated(OmcReportItemAddValidator.class) @RequestBody ReportItemDto reportItemDto,
                              BindingResult br) {

        Integer result = saveReportItem(reportItemDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
    * 修改异常报告项
    * */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editService(@Validated(OmcReportItemEditValidator.class) @RequestBody ReportItemDto reportItemDto,
                               BindingResult br){

        Integer result = saveReportItem(reportItemDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
    * 删除异常报告项
    * */
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteById/{reportItemId}")
    public Wrapper deleteService(@PathVariable Long reportItemId) {

        // 校验serviceItemId是否为空
        Preconditions.checkArgument(reportItemId != null, cmnyMessageSource
                .getMessage("reportItem.id.null", null));

        Integer result = omcReportItemService.deleteByKey(reportItemId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);

    }

    /*
    * 根据服务项id查询异常报告项列表
    * */
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryListByServiceItemId/{serviceItemId}")
    public Wrapper queryServiceItemList(@PathVariable Long serviceItemId) {

        List<ReportItemVo> reportItemVos = omcReportItemService.selectByServiceItemId(serviceItemId);
        //return WrapMapper.ok(reportItemVos);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),reportItemVos);
    }

    /*
    * 保存异常报告项
    * */
    public Integer saveReportItem(ReportItemDto reportItemDto) {
        OmcReportItem omcReportItem = new OmcReportItem();
        BeanUtils.copyProperties(reportItemDto, omcReportItem);
        return omcReportItemService.saveReportItem(omcReportItem);
    }


}
